unit EditTables;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, Oracle, uSQLs, uUtils, gnugettext, DB,
  OracleData, DBCtrls, umainutils;

type
  TuFrmEditTables = class(TForm)
    edt_Name: TLabeledEdit;
    edt_Seats: TLabeledEdit;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    btn_Close: TSpeedButton;
    OracleQuery1: TOracleQuery;
    chb_Disabled: TCheckBox;
    chb_CashGame: TCheckBox;
    com_Zone: TDBLookupComboBox;
    Label3: TLabel;
    OracleDataSetZone: TOracleDataSet;
    DataSourceZone: TDataSource;
    procedure btn_SaveClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btn_CloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    m_IsClone, m_IsEdit: boolean;
    m_recordID: string;
    procedure PopulateZoneCombo();
  public
    procedure ShowForm(IsEdit, IsClone: boolean; recordID: string);
    { Public declarations }
  end;

var
  uFrmEditTables: TuFrmEditTables;

implementation

uses UfrmMain;
{$R *.dfm}
{ TuFrmEditTables }

procedure TuFrmEditTables.btn_CloseClick(Sender: TObject);
begin
  close();
end;

procedure TuFrmEditTables.btn_SaveClick(Sender: TObject);
begin
  if m_IsClone or (not m_IsEdit) then
  begin
    InsertDataIntoDB('TABLES', 'NAME,SEATS,DISABLED,CASH_GAME,TABLE_ZONE_ID', '''' + edt_Name.Text + ''', ''' + edt_Seats.Text + ''', ''' + BToStr(chb_Disabled.Checked) + ''', ''' + BToStr
        (chb_CashGame.Checked) + ''', ''' + VarToStr(com_Zone.KeyValue) + '''');
  end
  else
  begin
    UpdateDataToDB('TABLES', 'TABLE_ZONE_ID=''' + VarToStr(com_Zone.KeyValue) + ''',NAME=''' + edt_Name.Text + ''',SEATS=''' + edt_Seats.Text + ''',DISABLED=''' + BToStr(chb_Disabled.Checked) + ''',CASH_GAME=''' + BToStr(chb_CashGame.Checked)
        + '''', 'ID=' + m_recordID);
  end;
  FrmMain.GetGridData(FrmMain.stg_Tables, SQL_Tables, true);
  close();
end;

procedure TuFrmEditTables.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close
  else if Key = #13 then
    btn_Save.Click;
end;

procedure TuFrmEditTables.FormShow(Sender: TObject);
begin
  edt_Name.SetFocus;
end;

procedure TuFrmEditTables.PopulateZoneCombo();
begin
  OracleDataSetZone.CloseAll;
  OracleDataSetZone.Session := FrmMain.OracleSession1;
  OracleDataSetZone.SQL.Clear;
  OracleDataSetZone.SQL.Text := ('select * from TABLE_ZONES order by NAME ASC');
  // ---
  OracleDataSetZone.Open;
  DataSourceZone.DataSet := OracleDataSetZone;
  com_Zone.ListSource := DataSourceZone;
  com_Zone.KeyField := 'ID';
  com_Zone.ListField := 'NAME';
  com_Zone.KeyValue := null;
end;

procedure TuFrmEditTables.ShowForm(IsEdit, IsClone: boolean; recordID: string);
var
  Query: string;
begin
  m_IsClone := IsClone;
  m_IsEdit := IsEdit;
  m_recordID := recordID;
  OracleQuery1.Session := FrmMain.OracleSession1;
  edt_Name.Text := '';
  edt_Seats.Text := '';
  chb_Disabled.Checked := false;
  chb_CashGame.Checked := false;
  PopulateZoneCombo();
  if IsEdit then
  begin
    Query := 'select * from tables where id=' + recordID;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    edt_Name.Text := OracleQuery1.Field('NAME');
    edt_Seats.Text := OracleQuery1.Field('SEATS');
    chb_Disabled.Checked := StrToB(OracleQuery1.Field('DISABLED'));
    chb_CashGame.Checked := StrToB(OracleQuery1.Field('CASH_GAME'));
    com_Zone.KeyValue := OracleQuery1.Field('TABLE_ZONE_ID');
  end;
  ShowModal();
end;

end.
